package homework.gui;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoRadioButton;
import com.lovo.netCRM.component.LovoTxt;
import homework.bean.ClassBean;
import homework.bean.StudentBean;
import homework.util.DataUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
/**
 * @Author(作者) ys
 * @Description(类的描述):  修改面板
 * @Date(创建时间)
 * @version(开发的版本):1.0
 **/
public class UpdateStudentDialog extends JDialog {
      //文本输入框
      private LovoTxt nameTxt,ageTxt;
      //单选按钮
     private LovoRadioButton genderRadio;
     //下拉菜单
    private LovoComboBox  classList;
    //修改按钮
    private LovoButton updateBtn;

    private StudentFrame studentFrame;

    private  StudentBean studentBean;

    public UpdateStudentDialog(StudentFrame studentFrame,Integer id){

        this.studentFrame = studentFrame;

        this.setSize(600,400);
        this.setLayout(null);

        //根据编号查询该编号的学生
         studentBean =DataUtil.getStudentById(id);

        nameTxt =  new LovoTxt("姓名",50,100,this);
        nameTxt.setText(studentBean.getName());
        ageTxt =  new LovoTxt("年龄",50,150,this);
        ageTxt.setText(studentBean.getAge()+"");
        genderRadio = new LovoRadioButton("性别",new String[]{"男","女"},50,200,this);
        genderRadio.setItem(studentBean.getGender());
        //查询所有的班级
        List<ClassBean> list = DataUtil.getAllClasses();
        classList = new LovoComboBox(list,"所在班级",50,250,this);
        //设置显示对应的班级
       // classList.setItem(studentBean.getClassBean().getClassId(),"id");
        updateBtn = new LovoButton("修改",50,300,this);

        //添加按钮
        updateBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //分别获得输入控制的值
                String name = nameTxt.getText();
                 int age = Integer.parseInt(ageTxt.getText());
                 String gender = genderRadio.getItem();
                 ClassBean classBean = (ClassBean) classList.getItem();
                 //封装学生对象
                studentBean.setName(name);
                studentBean.setGender(gender);
                studentBean.setAge(age);
                studentBean.setClassBean(classBean);

                //把封装好的对象保存到数据工具类中
                DataUtil.updateStudent(studentBean);
                //得到更新的数据
                List<StudentBean> newlist = DataUtil.getAllStudents();
                //更新表格的显示数据
                studentFrame.getShowTablePanel().getDatas().updateLovoTable(newlist);
                //销毁添加对话框
                UpdateStudentDialog.this.dispose();

            }
        });


        this.setLocationRelativeTo(null);
        this.setVisible(true);



    }
}
